﻿using AutoMapper;
using MediatR;
using TraceabilityApp_Api.Application.Command.DeepProcessing_深加工_;
using TraceabilityApp_Domain;
using TraceabilityApp_ErrorCode;
using TraceabilityApp_Infrastructure;

namespace TraceabilityApp_Api.Application.Handler.DeepProcessing_深加工_
{
    public class AddDeepProcessingHandler : IRequestHandler<AddDeepProcessingCommand, ApiResult<int>>
    {
        private readonly IBase<DeepProcessingModel> deepProcessingRepo;
        private readonly IMapper mapper;

        public AddDeepProcessingHandler(IBase<DeepProcessingModel> deepProcessingRepo,IMapper mapper)
        {
            this.deepProcessingRepo = deepProcessingRepo;
            this.mapper = mapper;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<int>> Handle(AddDeepProcessingCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            var deepProcessing=mapper.Map<DeepProcessingModel>(request);
            var r=deepProcessingRepo.Add(deepProcessing).Result;
            if (r > 0)
            {
                result.Data = r;
                result.Code=ApiEnum.OK;
                result.Msg = "添加深加工信息成功";
            }
            else
            {
                result.Code=ApiEnum.Fail;
                result.Data = r;
                result.Msg = "添加深加工信息失败";
            }
            return Task.FromResult(result);
        }
    }
}
